/**
 * 用户详情对话框
 */
var UserInfoDlg = {
    data: {
        deptId: "1111",
        deptName: ""
    }
};

layui.use(['layer', 'form', 'admin', 'laydate', 'ax', 'formSelects','element'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var laydate = layui.laydate;
    var layer = layui.layer;
    var formSelects = layui.formSelects;
    var element = layui.element;

    // 点击部门时
    $('#deptName').click(function () {
        var formName = encodeURIComponent("parent.UserInfoDlg.data.deptName");
        var formId = encodeURIComponent("parent.UserInfoDlg.data.deptId");
        var treeUrl = encodeURIComponent("/dept/tree");

        layer.open({
            type: 2,
            title: '部门选择',
            area: ['90%', '95%'],
            content: Feng.ctxPath + '/system/commonTree?formName=' + formName + "&formId=" + formId + "&treeUrl=" + treeUrl,
            end: function () {
                $("#deptId").val(UserInfoDlg.data.deptId);
                $("#deptName").val(UserInfoDlg.data.deptName);
            }
        });
    });

    // 添加表单验证方法
    form.verify({
        psw: [/^[\S]{6,12}$/, '密码必须6到12位，且不能出现空格'],
        repsw: function (value) {
            if (value !== $('#userForm input[name=password]').val()) {
                return '两次密码输入不一致';
            }
        }
    });

    //表单提交事件:发布
    form.on('submit(btnSubmitRelease)', function (data) {

        var fields=$(data.form).serialize();
        var alllist = fields.split("&")

        var ajax = new $ax(Feng.ctxPath + "/frame/frame_addAndRelese", function (data) {
            Feng.success("发布成功！");

            //传给上个页面，刷新table用
            admin.putTempData('formOk', true);

            // 关闭当前页面
            // var index = parent.layer.getFrameIndex(window.name);
            // parent.layer.close(index);

            //刷新父页面
            parent.location.reload();
            //关掉对话框
            admin.closeThisDialog();

        }, function (data) {
            Feng.error("发布失败！" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();

        //添加 return false 可成功跳转页面
        return false;
    });

    // 表单提交事件:存为草稿
    form.on('submit(btnSubmit)', function (data) {

        var fields=$(data.form).serialize();
        var alllist = fields.split("&")

        var ajax = new $ax(Feng.ctxPath + "/frame/frame_add", function (data) {
            Feng.success("存为草稿成功！");

            //传给上个页面，刷新table用
            admin.putTempData('formOk', true);

            // 关闭当前页面
            // var index = parent.layer.getFrameIndex(window.name);
            // parent.layer.close(index);

            //刷新父页面
            parent.location.reload();
            //关掉对话框
            admin.closeThisDialog();

        }, function (data) {
            Feng.error("存为草稿失败！" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();

        //添加 return false 可成功跳转页面
        return false;
    });

    var num =1;

    $(document).on('click','#addFrameitem',function () {
        num++;
        var html = '';

        html += '<div id="abc'+num+'">';
        html += '    <div class="layui-inline layui-col-md4">';
        html += '        <label class="layui-form-label">开始时间<span style="color: red;">*</span></label>';
        html += '        <div class="layui-input-block">';
        html += '            <input id="testabc" type="text" name="startTime[]" class="layui-input date-icon datetime" autocomplete="off" lay-verify="required" required />'
        html += '        </div>';
        html += '    </div>';
        html += '    <div class="layui-inline layui-col-md4">';
        html += '        <label class="layui-form-label">结束时间<span style="color: red;">*</span></label>';
        html += '        <div class="layui-input-block">';
        html += '            <input id="testabc" type="text" name="endTime[]" class="layui-input date-icon datetime" autocomplete="off" lay-verify="required" required />';
        html += '        </div>';
        html += '    </div>';
        html += '    <div class="layui-inline layui-col-md10">';
        html += '        <label class="layui-form-label">备注</label>';
        html += '        <div class="layui-input-block">';
        html += '            <input name="remark[]" placeholder="请输入备注" type="text" class="layui-input"  />';
        html += '        </div>';
        html += '    </div>';
        html += '    <input name="sort[]" value=' + num + ' type="hidden" class="layui-input"   />';
        html += '</div>';


        $("#frameitemNUm").append(html);
        if (num ==2){
            var deletebut = '<button type="button" id="deleteFrameitem" class="layui-btn ">删除最后一条</button>'
            $("#menu").append(deletebut);
        }
        form.render();
        timeAdd();
    });

    //点击删除最后一条
    $(document).on('click','#deleteFrameitem',function () {
        $("#abc"+num).remove();
        num--;
        if (num ==1){
            $("#deleteFrameitem").remove();
        }

    });


    //初始化时间框
    function timeAdd(){
        lay('.datetime').each(function() {
            //时间选择器
            laydate.render({
                elem: this
                ,trigger: 'click'
                ,type: 'time'
                ,format: 'HH:mm'
                ,done: function (value, date, endDate) {
                    console.log(value); //得到日期生成的值，如：2017-08-18
                }
            });
        });
    }

    timeAdd();






});